<template>
  <div class="add">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 步骤栏 -->
    <el-steps :active="activeName-0" finish-status="success">
      <el-step title="步骤 1"></el-step>
      <el-step title="步骤 2"></el-step>
      <el-step title="步骤 3"></el-step>
      <el-step title="步骤 4"></el-step>
      <el-step title="步骤 5"></el-step>
    </el-steps>
    <!-- 标签栏 -->
    <el-form
      :model="addGoodsForm"
      :rules="rules"
      ref="addGoodsForm"
      :label-width="'120px'"
      style="margin:15px"
    >
      <el-tabs v-model="activeName" tab-position="left">
        <el-tab-pane label="基本信息" name="1">
          <el-form-item label="商品名称">
            <el-input v-model="addGoodsForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格">
            <el-input v-model="addGoodsForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量">
            <el-input v-model="addGoodsForm.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品数量">
            <el-input v-model="addGoodsForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品分类">
            <el-cascader
              expand-trigger="hover"
              :options="cateList"
              v-model="addGoodsForm.goods_cat"
              :props="props"
              clearable
              @change="getCate"
            ></el-cascader>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="商品参数" name="2">商品参数</el-tab-pane>
        <el-tab-pane label="商品属性" name="3">商品属性</el-tab-pane>
        <el-tab-pane label="商品图片" name="4">
          <el-upload
            class="upload-demo"
            action="http://localhost:8888/api/private/v1/upload"
            :headers="tokens()"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :file-list="goodsImg"
            list-type="picture"
            :before-upload="img"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-tab-pane>
        <el-tab-pane label="商品内容" name="5">
          <quill-editor
            v-model="addGoodsForm.goods_introduce"
            ref="myQuillEditor"
            :options="editorOption"
            style='height:400px;border-bottom:1px solid #ccc'
          ></quill-editor>
        </el-tab-pane>
      </el-tabs>
    </el-form>

    <el-button type="primary" @click="addGood">添加</el-button>
  </div>
</template>
<script>
import { getCateList, addGoods } from '@/api/categories_api.js'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  data () {
    return {
      // 图片上传
      goodsImg: [],
      activeName: '0',
      cateList: [],
      addGoodsForm: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: '',
        goods_cat: '',
        pics: [],
        goods_introduce: ''
      },
      props: {
        checkstrictly: true,
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      rules: {}
    }
  },
  // 引入富文本插件
  components: {
    quillEditor
  },
  mounted () {
    this.init()
  },
  methods: {
    getCate (value) {
      this.addGoodsForm.goods_cat = value.join(',')
      console.log(this.addGoodsForm.goods_cat)
    },
    addGood () {
      addGoods(this.addGoodsForm)
        .then(res => {
          if (res.data.meta.status === 201) {
            this.$message({
              type: 'warning',
              message: res.data.meta.msg
            })
            this.$router.push({ name: 'list' })
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 取到token
    tokens () {
      let token = localStorage.getItem('heima_token')
      return { Authorization: token }
    },
    // 图片上传成功后的钩子函数
    handleSuccess (response, file, fileList) {
      // console.log(response)
      this.addGoodsForm.pics.push({ pic: response.data.tmp_path })
    },
    // 文件上传之前的钩子函数
    img (file) {
      if (file.type.indexOf('image/') === -1) {
        this.$message({
          type: 'warning',
          message: '请选择图片格式的文件'
        })
      }
    },
    // 删除图片
    handleRemove (file, fileList) {
      if (file.response) {
        for (let i = 0; i < this.addGoodsForm.pics.length; i++) {
          if (this.addGoodsForm.pics[i].pic === file.response.data.tmp_path) {
            this.addGoodsForm.pics.splice(i, 1)
          }
        }
      }
    },
    init () {
      getCateList()
        .then(res => {
          // console.log(res)
          if (res.data.meta.status === 200) {
            this.cateList = res.data.data
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
